package free.order.security.service;


import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import free.order.security.dao.ResourceDao;
import free.order.security.dao.RoleDao;
import free.order.security.dao.UserDao;
import free.order.security.entity.Resource;
import free.order.security.entity.Role;
import free.order.security.entity.User;
import free.order.security.service.ServiceException;


/**
 * 整个User模块内的业务逻辑Facade类.
 * 组合user,role两者的DAO.DAO均直接使用泛型的SimpleHibernateTemplate.
 * 使用Spring的@Service/@Autowired 指定IOC设置.
 * 使用Spring的@Transactional指定事务管理.
 * 
 * @author stan
 */
@Service
@Transactional
public class SecurityService {

//	private static String AUTH_HQL = "select count(u) from User u where u.loginName=? and u.password=?";

	private UserDao userDao;

	private RoleDao roleDao;

	private ResourceDao resDao;

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public RoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public ResourceDao getResDao() {
		return resDao;
	}

	public void setResDao(ResourceDao resDao) {
		this.resDao = resDao;
	}

	@Transactional(readOnly = true)
	public User getUser(Long id) {
		return userDao.get(id);
	}

//	@Transactional(readOnly = true)
//	public Page<User> getAllUsers(Page<User> page) {
//		return userDao.findAll(page);
//	}

//	@Transactional(readOnly = true)
//	public User getUserByLoginName(String loginName) {
//		return userDao.findUniqueByProperty("loginName", loginName);
//	}

	public void saveUser(User user) {
		userDao.save(user);
	}

	public void deleteUser(Long id) {
		if (id == 1)
			throw new ServiceException("不能删除超级用户");

		User user = userDao.get(id);
		userDao.delete(user);
	}

//	@Transactional(readOnly = true)
//	public boolean isLoginNameUnique(String loginName, String orgLoginName) {
//		return userDao.isPropertyUnique("loginName", loginName, orgLoginName);
//	}

	/**
	 * 验证用户名密码. 
	 * 因为使用acegi做安全管理,此函数已作废,仅用作demo.
	 * 
	 * @return 验证通过时返回true.用户名或密码错误返回false.
	 */
	@Transactional(readOnly = true)
	public boolean auth(String loginName, String password) {
		if (!StringUtils.hasText(loginName) || !StringUtils.hasText(password))
			return false;

//		return (userDao.findLong(AUTH_HQL, loginName, password) == 1);
		return true;
	}

//	@Transactional(readOnly = true)
//	public List<Role> getAllRoles() {
//		return roleDao.findAll();
//	}

	@Transactional(readOnly = true)
	public Role getRole(Long id) {
		return roleDao.get(id);
	}

	public List<Role> getAllRoles(){
		return roleDao.getAll();
	}
	
	public void saveRole(Role role) {
		roleDao.save(role);
	}

//	public void deleteRole(Long id) {
//		if (id == 1)
//			throw new ServiceException("不能删除超级管理员角色");
//		Role role = roleDao.get(id);
//		roleDao.delete(role);
//	}

//	@Transactional(readOnly = true)
//	public List<Authority> getAllAuths() {
//		return authDao.findAll();
//	}

	@Transactional(readOnly = true)
	public Resource getAuthority(Long id) {
		return resDao.get(id);
	}

	public User getUserByLoginID(String loginID) {
		List<User> lstUser = userDao.findBy("loginid", loginID);
        if (lstUser == null || lstUser.isEmpty())
        {
            return null;
        }
        return lstUser.get(0); 
	}

	public List<User> getAllUsers() {
		return userDao.getAll();
	}
}
